package com.bulletelc.apollopre.module.deal.clearing;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.CartListBean;
import com.bulletelc.apollopre.entity.InvoiceSelectBean;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.module.main.center.invoice.list.InvoiceActivity;
import com.bulletelc.apollopre.utils.MyUtil;

import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/19
 * desc  : 结算页的adapter
 */

public class ClearingListAdapter extends BaseExpandableListAdapter implements View.OnClickListener,
        CompoundButton.OnCheckedChangeListener {
    private Context context;
    private List<CartListBean> orderList;
    private LayoutInflater inflater;
    private static final int CHILD_TYPE_PRODUCT = 0;
    private static final int CHILD_TYPE_FOOTER = 1;
    private OnInstallCheckChangeListener onInstallCheckChangeListener;

    public ClearingListAdapter(Context context, List<CartListBean> orderList) {
        this.context = context;
        this.orderList = orderList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        if (orderList != null) {
            return orderList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (orderList != null) {
            return orderList.get(groupPosition).getProduct_list().size() + 1;
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return orderList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return orderList.get(groupPosition).getProduct_list().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public int getChildTypeCount() {
        return 2;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        if (childPosition == (getChildrenCount(groupPosition)) - 1) {
            return 1;
        }
        return 0;
    }



    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderParent holderParent;
        if (convertView == null) {
            holderParent = new ViewHolderParent();
            convertView = inflater.inflate(R.layout.clearing_parent_layout, parent, false);
            holderParent.tvClearingParent = convertView.findViewById(R.id.tvClearingParent);
            convertView.setTag(holderParent);
        } else {
            holderParent = (ViewHolderParent) convertView.getTag();
        }

        holderParent.tvClearingParent.setText(orderList.get(groupPosition).getSupplier_name());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderChild holderChild = null;
        ViewHolderChildFooter childFooter = null;
        int type = getChildType(groupPosition, childPosition);

        if (convertView == null) {
            switch (type) {
                case CHILD_TYPE_PRODUCT:
                    holderChild = new ViewHolderChild();
                    convertView = inflater.inflate(R.layout.clearing_child_layout, parent, false);
                    holderChild.ivProductImg = convertView.findViewById(R.id.ivProductImg);
                    holderChild.tvProductName = convertView.findViewById(R.id.tvProductName);
                    holderChild.tvSku = convertView.findViewById(R.id.tvSku);
                    holderChild.tvProductPrice = convertView.findViewById(R.id.tvProductPrice);
                    holderChild.tvProductNumShow = convertView.findViewById(R.id.tvProductNumShow);
                    holderChild.tvInstallFee = convertView.findViewById(R.id.tvInstallFee);
                    convertView.setTag(R.id.tag_clearing_child, holderChild);
                    break;
                case CHILD_TYPE_FOOTER:
                    childFooter = new ViewHolderChildFooter();
                    convertView = inflater.inflate(R.layout.clearing_child_footer_layout, parent, false);
                    childFooter.tvSubNum = convertView.findViewById(R.id.tvSubNum);
                    childFooter.tvSubtotal = convertView.findViewById(R.id.tvSubtotal);
                    childFooter.rlBill = convertView.findViewById(R.id.rlBill);

                    childFooter.tvBillType = convertView.findViewById(R.id.tvBillType);

                    childFooter.tvInstallFee = convertView.findViewById(R.id.tvInstallFee);
                    childFooter.rlInstallPanel = convertView.findViewById(R.id.rlInstallPanel);
                    childFooter.cbInstall = convertView.findViewById(R.id.cbInstall);

                    childFooter.rlBill.setOnClickListener(this);
                    childFooter.cbInstall.setOnCheckedChangeListener(this);

                    convertView.setTag(R.id.tag_clearing_child_footer, childFooter);
                    break;
            }

        } else {
            switch (type) {
                case CHILD_TYPE_PRODUCT:
                    holderChild = (ViewHolderChild) convertView.getTag(R.id.tag_clearing_child);
                    break;

                case CHILD_TYPE_FOOTER:
                    childFooter = (ViewHolderChildFooter) convertView.getTag(R.id.tag_clearing_child_footer);
                    break;
            }


        }

        switch (type) {
            case CHILD_TYPE_PRODUCT:
                CartListBean.CartProductListBean cartProductListBean = orderList.get(groupPosition).getProduct_list().get(childPosition);
                holderChild.tvProductName.setText(cartProductListBean.getName());
                holderChild.tvSku.setText(cartProductListBean.getProduct_sku_title());
                holderChild.tvProductPrice.setText("¥" + MyUtil.format2(cartProductListBean.getPrice()));
                holderChild.tvProductNumShow.setText("x" + cartProductListBean.getQuantity());
                holderChild.tvInstallFee.setText("安装费：" + "¥" + MyUtil.format2(cartProductListBean.getInstall_fee()));
                ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holderChild.ivProductImg, cartProductListBean.getLogo_url()));
                break;

            case CHILD_TYPE_FOOTER:
                double installFee = orderList.get(groupPosition).getSub_install_fee();
                childFooter.tvSubNum.setText("共"+getSubNum(groupPosition)+"件商品");
                childFooter.tvSubtotal.setText("¥" + MyUtil.format2(orderList.get(groupPosition).getSub_total()));
                if (installFee != 0) {
                    childFooter.rlInstallPanel.setVisibility(View.VISIBLE);
                    childFooter.tvInstallFee.setText("¥" + MyUtil.format2(installFee));
                }
                if (orderList.get(groupPosition).getInvoice_id() == 0) {
                    childFooter.tvBillType.setText("请选择");
                } else {
                    childFooter.tvBillType.setText(orderList.get(groupPosition).getInvoice_name());
                }
                childFooter.rlBill.setTag(R.id.tag_clearing_bill_group,groupPosition);
                childFooter.rlBill.setTag(R.id.tag_clearing_bill_child,childPosition);
                childFooter.cbInstall.setTag(R.id.tag_clearing_install_group, groupPosition);
                childFooter.cbInstall.setTag(R.id.tag_clearing_install_child, childPosition);
                break;
        }

        return convertView;
    }


    /**
     * 设置子列表是否可选中,返回true可以选中
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlBill:
                int groupPosition=(int)v.getTag(R.id.tag_clearing_bill_group);
                int childPosition=(int)v.getTag(R.id.tag_clearing_bill_child);
                InvoiceSelectBean invoiceSelectBean = new InvoiceSelectBean();
                invoiceSelectBean.setGroupPosition(groupPosition);
                invoiceSelectBean.setChildPosition(childPosition);
                Intent intent = new Intent(context, InvoiceActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_FROM_CLEARING,invoiceSelectBean);
                ((Activity)context).startActivityForResult(intent,IntentCode.REQUEST_CODE_CLEARING_TO_INVOICE);
                break;

        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        int gp = (int) compoundButton.getTag(R.id.tag_clearing_install_group);
        if (onInstallCheckChangeListener != null) {
            onInstallCheckChangeListener.checkInstall(gp, b);
        }
    }

    private static class ViewHolderParent {
        TextView tvClearingParent;
    }

    private static class ViewHolderChild {
        ImageView ivProductImg;
        TextView tvProductName;
        TextView tvSku;
        TextView tvProductPrice;
        TextView tvProductNumShow;
        TextView tvInstallFee;
    }

    private static class ViewHolderChildFooter {
        TextView tvSubNum;
        TextView tvSubtotal;
        RelativeLayout rlBill;
        TextView tvBillType;
        TextView tvInstallFee;
        CheckBox cbInstall;
        RelativeLayout rlInstallPanel;
    }

    private int getSubNum(int groupPosition) {
        int num=0;
        List<CartListBean.CartProductListBean> listBeans = orderList.get(groupPosition).getProduct_list();
        for (int i = 0, size = listBeans.size(); i < size; i++) {
            num+=listBeans.get(i).getQuantity();
        }
        return num;
    }

    interface OnInstallCheckChangeListener {
        void checkInstall(int groupPos, boolean b);
    }

    public void setOnInstallCheckChangeListener(OnInstallCheckChangeListener listener) {
        this.onInstallCheckChangeListener = listener;
    }

}
